/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.core.util;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import com.cred.industries.core.exceptions.ApplicationException;

/**
 * helps build exception responses
 */
public final class ExceptionResponse {
	
	/**
	 * The constructor is private since this is a utility class 
	 * that does not need to be instantiated 
	 */
	private ExceptionResponse() {
		
	}

	/**
	 * helps build exception responses, with the message from the exception
	 * @param e exception, returns a default of an INTERNAL_SERVER_ERROR
	 * @return response to return to the client
	 */
	public static Response build(Exception e) {
		ResponseBuilder response = Response.status(Status.INTERNAL_SERVER_ERROR);
		response.type(MediaType.TEXT_PLAIN);
		response.entity(e.getMessage());
		return response.build();
	}
	
	/**
	 * helps build exception responses, with the message from the exception
	 * @param e Throwable, returns a default of an INTERNAL_SERVER_ERROR
	 * @return response to return to the client
	 */
	public static Response build(Throwable e) {
		ResponseBuilder response = Response.status(Status.INTERNAL_SERVER_ERROR);
		response.type(MediaType.TEXT_PLAIN);
		response.entity(e.getMessage());
		return response.build();
	}
	
	/**
	 * helps build exception responses, with the message from the exception
	 * @param e Exception
	 * @param statusCode the status we want to return to the clients 
	 * @return response to return to the client
	 */
	public static Response build(Exception e, Status statusCode) {
		ResponseBuilder response = Response.status(statusCode);
		response.type(MediaType.TEXT_PLAIN);
		response.entity(e.getMessage());
		return response.build();
	}
	
	/**
	 * helps build exception responses, with the message from the exception
	 * @param e ApplicationException
	 * @param statusCode the status we want to return to the clients 
	 * @return response to return to the client
	 */
	public static Response build(ApplicationException e, Status statusCode) {
		ResponseBuilder response = Response.status(statusCode);
		response.type(MediaType.TEXT_PLAIN);
		response.entity(e.getErrorCondition());
		return response.build();
	}
	
	/**
	 * helps build exception responses, with the message from the exception
	 * @param e ApplicationException, we get the message and the status code from the ApplicationException
	 * @return response to return to the client
	 */
	public static Response build(ApplicationException e) {
		ResponseBuilder response = Response.status(e.getStatusCode());
		response.type(MediaType.TEXT_PLAIN);
		response.entity(e.getErrorCondition());
		return response.build();
	}
	
	/**
	 * helps build exception responses with a status code and a message provided
	 * @param statusCode the status we want to return to the clients 
	 * @param message the message we want to return to the client
	 * @return response to return to the client
	 */
	public static Response build(Status statusCode, String message) {
		ResponseBuilder response = Response.status(statusCode);
		response.type(MediaType.TEXT_PLAIN);
		response.entity(message);
		return response.build();
	}
	
	/**
	 * helps build exception responses with a status code, no message
	 * @param statusCode the status we want to return to the clients 
	 * @return response to return to the client
	 */
	public static Response build(Status statusCode) {
		return build(statusCode, "");
	}

}
